﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HYSoft.Web
{
    public partial class superadmin: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["touid"] == null)
                uidnull();
            else
            {
                if (!IsPostBack)
                {
                    user_list();
                    data_list();
                    user_status();
                }
                nodetype_change();
                Session["utype"] = "88";
            }
        }
        private void user_list()
        {
            //comm_data da2 = new comm_data();
          //  user queryuser = new user();
            //DataTable dt2 = da2.backdatatalbeselect("select * from [user] where [type]=1");
            string sql = " [type]=1";
            DataTable dt2 = new BLL.user().GetList(sql).Tables[0];
           // DataTable dt2 = queryuser.GetList("[type]=1").Tables[0];
            if (dt2.Rows.Count > 0)
            {
                DropDownList4.DataSource = dt2.DefaultView;
                DropDownList4.DataTextField = "name";
                DropDownList4.DataValueField = "index";
                DropDownList4.DataBind();
                dt2.Clear();
                dt2.Dispose();
                DropDownList4.SelectedValue = Request.QueryString["touid"].ToString();
            }
        }

        private void user_status()
        {
            int uid2 = int.Parse(Request.QueryString["touid"].ToString());
            //comm_data da = new comm_data();
           // user queryuser = new user();
            //DataTable dt = da.chektable("[user]", "where [index] = " + uid2);
            string sql = " [index]="+uid2+"";
            DataTable dt = new BLL.user().GetList(sql).Tables[0];
            //DataTable dt = queryuser.GetList("[index] = " + uid2).Tables[0];
            if (dt.Rows.Count > 0)
            {
                //DropDownList1.SelectedIndex = int.Parse(dt.Rows[0]["intvalue1"].ToString());
                DropDownList2.SelectedIndex = int.Parse(dt.Rows[0]["intvalue2"].ToString());
            }
            dt.Clear();
            dt.Dispose();
        }

        private void uidnull()
        {
            //user queryuser = new user();
            string sql = " [type]=1";
            DataTable dt2 = new BLL.user().GetList(sql).Tables[0];
            //DataTable dt2 = queryuser.GetList("[type]=1").Tables[0];
            //DataTable dt2 = da2.backdatatalbeselect("select * from [user] where [type]=1 ");
            if (dt2.Rows.Count > 0)
            {
                Response.Write("<script>window.location = 'superadmin.aspx?touid=" + dt2.Rows[0]["index"] + "';</script>");
            }
            else
            {
                Response.Write("<script>alert('该单位还没有已注册普通用户！');history.back();</script>");
            }
        }

        protected void nodetype_change()
        {
            string nodetype = DropDownList4.SelectedValue.ToString();
            if (nodetype != Request.QueryString["touid"].ToString())
                Response.Write("<script>window.location = 'superadmin.aspx?touid=" + nodetype + "';</script>");
        }


        protected void data_list()
        {
            int uid2 = int.Parse(Request.QueryString["touid"].ToString());
            //comm_data da = new comm_data();
           // user queryuser = new user();
            //DataTable dt = da.chektable("[user]", "where [index] = " + uid2);
            string sql = " [index]="+uid2+"";
            DataTable dt = new BLL.user().GetList(sql).Tables[0];
            //DataTable dt = queryuser.GetList("[index] = " + uid2).Tables[0];
            if (dt.Rows.Count > 0)
            {
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
            }
            dt.Clear();
            dt.Dispose();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int uid2 = int.Parse(Request.QueryString["touid"].ToString());
            Model.user usermodel=new BLL.user().GetModel(uid2);
            if (usermodel != null)
            {
                usermodel.index = uid2;
                usermodel.intvalue2 = DropDownList2.SelectedIndex;
                if (new BLL.user().Update(usermodel))
                {
                    Response.Write("<script>alert('用户状态修改成功！');window.location = 'superadmin.aspx?touid=" + uid2 + "';</script>");
                }
                else
                {
                    Response.Write("<script>alert('用户状态修改失败！');window.location = 'superadmin.aspx?touid=" + uid2 + "';</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('用户状态修改失败,不存在该用户！');window.location = 'superadmin.aspx';</script>");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int uid2 = int.Parse(Request.QueryString["touid"].ToString());
            Response.Write("<script>alert('已放弃用户状态修改！');window.location = 'superadmin.aspx?touid=" + uid2 + "';</script>");
        }
    }
}